<template>
  <div class="login-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="login-form" autocomplete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" autocomplete="on" tabindex='1' ref="username">
            <svg-icon icon-class="yonghu" class="colorSvg" slot="prefix"/>
          </el-input>
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>

          <el-form-item prop="password">
            <el-input :type="passwordType" v-model="ruleForm.password" autocomplete="on" tabindex='2' ref="password"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="submitForm('ruleForm')"
            >
              <svg-icon icon-class="mima" class="colorSvg" slot="prefix"/>
            </el-input>

            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

        </el-tooltip>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="width:100%;">登 录</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
// import { userLogin } from '@/api/user'
export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: 'admin',
        password: '1111111'
      },
      rules: {
        username: [{ required: true, validator: validateUsername, trigger: 'blur' }],
        password: [{ required: true, validator: validatePassword, trigger: 'blur' }]
      },
      capsTooltip: false,
      passwordType: 'password',
      redirect: undefined,
      loading: false,
      otherQuery: {}
    }
  },
  // components: { IconSvg },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted () {
    if (this.ruleForm.username === '') {
      this.$refs.username.focus()
    } else if (this.ruleForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    checkCapslock ({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if ((shiftKey && (key >= 'A' && key <= 'Z')) || (!shiftKey && (key >= 'A' && key <= 'Z'))) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === false) {
        this.capsTooltip = true
      } else if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },

    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
          this.loading = true
          this.$store.dispatch('user/userLogin', this.ruleForm)
            .then(res => {
            // 登录成功
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 啥玩意?
    getOtherQuery (query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        console.log(acc)
        return acc
      }, {})
    }
  }

}
</script>
<style lang="less" scoped>
@light_gray:#eee;
@bg:#2d3a4b;
@dark_gray:#889aa4;

.login-container{
  width: 100%;
  min-height: 100%;
  background-color: @bg;
  overflow: hidden;
}
.login-form{
  position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
}
.title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: @light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
.colorSvg{
  color:@dark_gray;
}
.show-pwd{
    position: absolute;
    right: 10px;
    top: 0px;
    font-size: 16px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
}
.el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
}
</style>
